package com.ptit.piclient;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.ptit.piclient.model.Song;
import com.ptit.piclient.tools.AllSongAdapter2;
import com.ptit.piclient.tools.PiConnector;

public class AllSongActivity extends Activity implements OnItemClickListener,
		TextWatcher {
	ListView lv;
	EditText txtSearch;
	ArrayList<Song> list = new ArrayList<Song>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_song_list);
		lv = (ListView) findViewById(R.id.allSong);
		lv.setOnItemClickListener(this);
		txtSearch = (EditText) findViewById(R.id.txtSearch);
		txtSearch.addTextChangedListener(this);
		new UpdateView(this).execute("");
	}

	private class AudioLibraryScan extends AsyncTask<Void, Void, Void> {
		private Activity act;

		public AudioLibraryScan(Activity a) {
			act = a;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			PiConnector.getInstance(act).audioLibraryScan();
			new UpdateView(act).execute();
			return null;
		}

		@Override
		protected void onProgressUpdate(Void... params) {
			ProgressDialog dialog = new ProgressDialog(AllSongActivity.this);// ,
																				// "",
																				// "Please wait...",true);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();

		}

		// khuongdv start sep28
		@Override
		protected void onPostExecute(Void result) {

		}
		// khuongdv end sep28
	}

	/*
	 * New thread to update listview
	 */
	private class UpdateView extends AsyncTask<String, Void, AllSongAdapter2> {
		Activity act;
		ProgressDialog dialog;

		public UpdateView(Activity a) {
			act = a;
			dialog = new ProgressDialog(AllSongActivity.this);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();
		}

		@Override
		protected AllSongAdapter2 doInBackground(String... params) {
			// Oct 11
			if (params[0].equals("") == false)
				dialog.dismiss();
//			 Oct11 end
			list = PiConnector.getInstance(act).getSongsByKeyword(params[0]);// getAllSongsFromPI();
			AllSongAdapter2 ad = new AllSongAdapter2(act, list);
			return ad;
		}

		@Override
		protected void onPreExecute() {

		}

		@Override
		protected void onProgressUpdate(Void... params) {
			ProgressDialog dialog = new ProgressDialog(AllSongActivity.this);// ,
																				// "",
																				// "Please wait...",true);
			dialog.setTitle("");
			dialog.setMessage("Please wait...");
			dialog.setIndeterminate(true);
			dialog.show();

		}

		protected void onPostExecute(AllSongAdapter2 result) {
			if(dialog.isShowing())
				dialog.dismiss();
			if (result != null)
				lv.setAdapter(result);
		}
	}

	/**
	 * Play a song that you 've clicked on it
	 * 
	 * @author Khuong
	 * 
	 */
	private class PlaySong extends AsyncTask<Integer, Void, Void> {
		Activity act;
		int position;

		public PlaySong(Activity a, int position) {
			act = a;
			this.position = position;
		}

		@Override
		protected Void doInBackground(Integer... params) {
			try {
				PiConnector.getInstance(act).playsong(position);
				Toast.makeText(act, "" + position, 0).show();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPreExecute() {

		}

		protected void onPostExecute(Void result) {
			Intent intent = new Intent(act, NowPlayingActivity.class);
			startActivity(intent);
		}

	}

	// Oct 11
//	@Override
//	protected void onResume() {
//		super.onResume();
//		new UpdateView(this).execute(txtSearch.getText().toString());
//	}

	// Oct 11
	// =============END ASYNTASK
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// Oct 8
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuItemUpdateLibrary:
			updateLibrary();
			return true;

			// ... other item selection handling
		case R.id.menuItemGotoNowPlaying:
			gotoNowPlaying();
			return true;
		default:
			return super.onOptionsItemSelected(item);

		}
	}

	private void updateLibrary() {
		new AudioLibraryScan(this).execute();
	}

	private void gotoNowPlaying() {
		Intent i = new Intent(getApplicationContext(), NowPlayingActivity.class);
		// i.putExtra("isPlayingAllSong", false);
		startActivity(i);
	}

	// End oct 8
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// new PlaySong(this, list.get(position)).execute(); -- Oct 8
		new PlaySong(this, position).execute();
	}

	@Override
	public void afterTextChanged(Editable arg0) {

	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		new UpdateView(this).execute(txtSearch.getText().toString());
	}

}